package concurrent;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 通过提交给线程池来执行
 */

class MyWork implements Runnable{
    private int myCounter = 0;

    @Override
    public void run() {
        for(; myCounter < 20; myCounter++){
            if (Thread.currentThread().getName().endsWith("2")){
                System.out.println("\t\t"+Thread.currentThread().getName() + ", " + myCounter);
            }else {
                System.out.println("\t"+Thread.currentThread().getName() + ", " + myCounter);
            }
        }
    }
}
public class ExecutorServicePool4 {
    public static void main(String[] args) {
        ExecutorService pool = Executors.newFixedThreadPool(5);
        MyWork work = new MyWork();

        //同一个target类被传给了两个Thread对象，两个线程完成这个任务，他们共享一个myCounter成员变量
        pool.submit(new Thread(work, "thread1"));
        pool.submit(new Thread(work),"thread2");
//        pool.submit(new MyThread());
//        pool.submit(new MyThread());
        pool.shutdown();

    }
}
